package club.dataresources.service.impl;

import club.dataresources.entity.Customer;
import club.dataresources.entity.CustomerExample;
import club.dataresources.mapper.CustomerMapper;
import club.dataresources.service.CustomerService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author
 * @create 2018-11-12 10:32
 **/
@Service
public class CustomerServiceImpl implements CustomerService{
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public boolean addCustomer(Customer customer) throws Exception {
        return customerMapper.insertSelective(customer)==1;
    }

    @Override
    public boolean removeCustomer(Integer id) throws Exception {
        return customerMapper.deleteByPrimaryKey(id)==1;
    }

    @Override
    public boolean updateCustomer(Customer customer) throws Exception {
        return customerMapper.updateByPrimaryKeySelective(customer)==1;
    }

    @Override
    public Customer findCuById(Integer id) throws Exception {
        return customerMapper.selectByPrimaryKey(id);
    }

    @Override
    public Page<Customer> listCustomer(Integer page, Integer rows, Customer customer) throws Exception {
        PageHelper.startPage(page, rows);
        CustomerExample example = new CustomerExample();
        CustomerExample.Criteria criteria = example.createCriteria();
        if(customer.getName()!=null){
            criteria.andNameLike(customer.getName());
        }
        if(customer.getIsMember()!=null){
            criteria.andIsMemberEqualTo(customer.getIsMember());
        }
        if(customer.getIsStudent()!=null){
            criteria.andIsStudentEqualTo(customer.getIsStudent());
        }
        if(customer.getPhone()!=null){
            criteria.andPhoneLike(customer.getPhone());
        }
        criteria.andIdIsNotNull();
        return customerMapper.selectByExample(example);
    }
}
